﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Radiation___IM.ApiCommands
{
    public class Login : ICommand
    {
        private readonly string m_commandkey = Radiation.Common.Communication.APIMethods.LOGIN;

        public Login()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return "login";
        }

        public void execute(string[] commands, Client client)
        {
            if (commands.Length >= 4 &&
                commands[2].Length > 1 &&
                commands[3].Length > 1)
            {
                // Continue
                if (Users.UserManager.Instance[commands[2]] != null)
                {
                    if (Users.UserManager.Instance[commands[2]].Password == commands[3])
                    {
                        MessageExecution.sendDataAPI(client, Radiation.Common.Communication.Constants.LOGIN_SUCESS, Radiation.Common.Communication.APIMethods.LOGIN);
                        Thread.Sleep(200);
                        Commands.Nick.changeNick(commands[2], client);
                    }
                    else
                        MessageExecution.sendDataAPI(client, Radiation.Common.Communication.Constants.LOGIN_BAD_PASSWORD, Radiation.Common.Communication.APIMethods.LOGIN);
                }
                else
                    MessageExecution.sendDataAPI(client, Radiation.Common.Communication.Constants.LOGIN_BAD_NICKNAME, Radiation.Common.Communication.APIMethods.LOGIN);
            }
            else
                MessageExecution.sendDataAPI(client, Radiation.Common.Communication.Constants.LOGIN_ERROR, Radiation.Common.Communication.APIMethods.LOGIN);
        }
    }
}
